package cn.cena.org.service.impl;


import cn.cena.basic.Exception.MyException;
import cn.cena.basic.service.impl.BaseServiceImpl;
import cn.cena.org.domian.Employee;
import cn.cena.org.domian.Shop;
import cn.cena.org.mapper.EmployeeMapper;
import cn.cena.org.mapper.ShopMapper;
import cn.cena.org.service.IShopService;
import cn.cena.user.domain.Logininfo;
import cn.cena.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void settledIn(Shop shop) {
        // 校验用户
        Employee admin = shop.getAdmin();
        Employee employee = employeeMapper.findByUsername(admin.getUsername());
        if (employee != null){ // 用户名存在
            throw new MyException("用户名重复");
        }
        // 用户名不存在,保存信息，并返回自增id
        employeeMapper.save(admin);
        shopMapper.save(shop);
        // 管理员关联店铺id
        admin.setShop_id(shop.getId());
        System.err.println(shop.getId());
        employeeMapper.update(admin);

        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin, logininfo);
        logininfo.setType(0);
        logininfoMapper.save(logininfo);
    }
}
